home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / teach / texture / tex3d.c.z / tex3d.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  4.6 KB  |  177 lines

  1. /*
  2.  * tex3d - simple 3D texturing program
  3.  *
  4.  * Shows a 3D texture by drawing slices through it.
  5.  */
  6. /* compile: cc -o tex3d tex3d.c -lGLU -lGL -lX11 */
  7.  
  8. #include <GL/glx.h>
  9. #include <GL/glu.h>
  10. #include <X11/keysym.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13.  
  14. static int attributeList[] = { GLX_RGBA, None };
  15.  
  16. unsigned int tex[64][64][64];
  17.  
  18. /* generate a simple 3D texture */
  19. static void
  20. make_texture(void) {
  21.     int i, j, k;
  22.     unsigned int *p = &tex[0][0][0];
  23.  
  24.     for (i=0; i<64; i++) {
  25.     for (j=0; j<64; j++) {
  26.         for (k=0; k<64; k++) {
  27.         if (i < 10 || i > 48 ||
  28.             j < 10 || j > 48 ||
  29.             k < 10 || k > 48) {
  30.             if (i < 2 || i > 62 ||
  31.             j < 2 || j > 62 ||
  32.             k < 2 || k > 62) {
  33.             *p++ = 0x00000000;
  34.             } else {
  35.             *p++ = 0xff80ffff;
  36.             }
  37.         } else {
  38.             *p++ = 0x000000ff;
  39.         }
  40.         }
  41.     }
  42.     }
  43. }
  44.  
  45. static void
  46. init(void) {
  47.     make_texture();
  48.     glEnable(GL_TEXTURE_3D_EXT);
  49.     glEnable(GL_BLEND);
  50.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  51.     glClearColor(0.2,0.2,0.5,1.0);
  52.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  53.  
  54.     glMatrixMode(GL_PROJECTION);
  55.     gluPerspective(60.0, 1.0, 1.0, 100.0 );
  56.     glMatrixMode(GL_MODELVIEW);
  57.     glTranslatef(0.,0.,-3.0);
  58.     glMatrixMode(GL_TEXTURE);
  59.  
  60.     /* Similar to defining a 2D texture, but note the setting of the */
  61.     /* wrap parameter for the R coordinate.  Also, for 3D textures   */
  62.     /* you probably won't need mipmaps, hence the linear min filter. */
  63.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  64.     glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  65.     glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP);
  66.     glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP);
  67.     glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_R_EXT, GL_CLAMP);
  68.     glTexImage3DEXT(GL_TEXTURE_3D_EXT, 0, 4, 64, 64, 64, 0,
  69.             GL_RGBA, GL_UNSIGNED_BYTE, tex);
  70. }
  71.  
  72. #define NUMSLICES 256
  73.  
  74. static void
  75. draw_scene(void) {
  76.     int i;
  77.     float r, dr, z, dz;
  78.     
  79.     glColor4f(1, 1, 1, 1.4/NUMSLICES);
  80.     glClear(GL_COLOR_BUFFER_BIT);
  81.  
  82.     /* Display the entire 3D texture by drawing a series of quads that  */
  83.     /* slice through the texture coordinate space.  Note that the       */
  84.     /* transformations below are applied to the texture matrix, not the */
  85.     /* modelview matrix. */
  86.        
  87.     glLoadIdentity();
  88.     /* center the texture coords around the [0,1] cube */
  89.     glTranslatef(.5,.5,.5);
  90.     /* a rotation just to make the picture more interesting */
  91.     glRotatef(45.,1.,1.,.5);
  92.  
  93.     /* to make sure that the texture coords, after arbitrary rotations, */
  94.     /* still fully contain the [0,1] cube, make them span a range       */
  95.     /* sqrt(3)=1.74 wide */
  96.     r = -0.87; dr = 1.74/NUMSLICES;
  97.     z = -1.00; dz = 2.00/NUMSLICES;
  98.     for (i=0; i < NUMSLICES; i++) {
  99.     glBegin(GL_TRIANGLE_STRIP);
  100.     glTexCoord3f(-.87,-.87,r); glVertex3f(-1,-1,z); 
  101.     glTexCoord3f(-.87, .87,r); glVertex3f(-1, 1,z); 
  102.     glTexCoord3f( .87,-.87,r); glVertex3f( 1,-1,z); 
  103.     glTexCoord3f( .87, .87,r); glVertex3f( 1, 1,z); 
  104.     glEnd();
  105.     r += dr;
  106.     z += dz;
  107.     }
  108. }
  109.  
  110. static void
  111. process_input(Display *dpy) {
  112.     XEvent event;
  113.     Bool redraw = 0;
  114.  
  115.     do {
  116.     char buf[31];
  117.     KeySym keysym;
  118.  
  119.     XNextEvent(dpy, &event);
  120.     switch(event.type) {
  121.     case Expose:
  122.         redraw = 1;
  123.         break;
  124.     case ConfigureNotify:
  125.         glViewport(0, 0, event.xconfigure.width, event.xconfigure.height);
  126.         redraw = 1;
  127.         break;
  128.     case KeyPress:
  129.         (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
  130.         switch (keysym) {
  131.         case XK_Escape:
  132.         exit(EXIT_SUCCESS);
  133.         default:
  134.         break;
  135.         }
  136.     default:
  137.         break;
  138.     }
  139.     } while (XPending(dpy));
  140.     if (redraw) draw_scene();
  141. }
  142.  
  143. static void
  144. error(const char *prog, const char *msg) {
  145.     fprintf(stderr, "%s: %s\n", prog, msg);
  146.     exit(EXIT_FAILURE);
  147. }
  148.  
  149. int
  150. main(int argc, char **argv) {
  151.     Display *dpy;
  152.     XVisualInfo *vi;
  153.     XSetWindowAttributes swa;
  154.     Window win;
  155.     GLXContext cx;
  156.  
  157.     dpy = XOpenDisplay(0);
  158.     if (!dpy) error(argv[0], "can't open display");
  159.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
  160.     if (!vi) error(argv[0], "no suitable visual");
  161.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  162.  
  163.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  164.                                    vi->visual, AllocNone);
  165.     swa.border_pixel = 0;
  166.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  167.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 400, 400,
  168.             0, vi->depth, InputOutput, vi->visual,
  169.             CWBorderPixel|CWColormap|CWEventMask, &swa);
  170.     XStoreName(dpy, win, "tex3d");
  171.     XMapWindow(dpy, win);
  172.     glXMakeCurrent(dpy, win, cx);
  173.  
  174.     init();
  175.     while (1) process_input(dpy);
  176. }
  177.